package przykladyWykresow;

import java.awt.Color;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 * A simple demonstration application showing how to create a dual axis chart
 * based on data from two {@link CategoryDataset} instances.
 * 
 */
@SuppressWarnings("serial")
public class WykresSlupkowy extends ChartPanelAbstract {

	/**
	 * Creates a new demo instance.
	 * 
	 * @param title
	 *            the frame title.
	 */
	static CategoryDataset dataset1 = null;
	static HashMap<String, Integer> hashmap1 = new LinkedHashMap<String, Integer>();
	static HashMap<String, Integer> hashmap2 = new LinkedHashMap<String, Integer>();
	static HashMap<String, Integer> hashmap3 = new LinkedHashMap<String, Integer>();
	static HashMap<String, Integer> hashmap4 = new LinkedHashMap<String, Integer>();
	static {
		String kateg = "Kategoria";
		for (int j = 0; j < 8; j++) {
			hashmap1.put(kateg + " " + j, random(40));
			hashmap2.put("" + j, random(40));
			hashmap3.put("" + j, random(40));
			hashmap4.put("" + j, random(40));
		}
	}
	private static String title = null;
	int total = 0;

	public WykresSlupkowy(String _title) {
		super(createChart(_title));
		setTitle(_title);

	}

	@SuppressWarnings("deprecation")
	public static JFreeChart createChart(String _title) {
		dataset1 = createDataset1();
		final JFreeChart chart = ChartFactory.createBarChart(_title, // chart
				// title
				"Category", // domain axis label
				"Value", // range axis label
				dataset1, // data
				PlotOrientation.VERTICAL, true, // include legend
				true, // tooltips?
				false // URL generator? Not required...
				);

		// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
		chart.setBackgroundPaint(Color.white);
		// chart.getLegend().setAnchor(Legend.SOUTH);

		// get a reference to the plot for further customisation...
		final CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);

		final CategoryDataset dataset2 = createDataset2();
		plot.setDataset(1, dataset2);
		plot.mapDatasetToRangeAxis(1, 1);

		final CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
		final ValueAxis axis2 = new NumberAxis("Wykres drugi");
		plot.setRangeAxis(1, axis2);

		final LineAndShapeRenderer renderer2 = new LineAndShapeRenderer();
		renderer2.setToolTipGenerator(new StandardCategoryToolTipGenerator());
		plot.setRenderer(1, renderer2);
		plot.setDatasetRenderingOrder(DatasetRenderingOrder.REVERSE);
		// OPTIONAL CUSTOMISATION COMPLETED.

		return chart;
	}

	private static CategoryDataset createDataset1() {

		// row keys...
		final String series1 = "Pierwszy";
		final String series2 = "Drugi";
		final String series3 = "Trzeci";

		// create the dataset...
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		int j = 0;
		for (String key : hashmap1.keySet()) {
			dataset.addValue(hashmap1.get(key), series1, key);
		}

		for (String key : hashmap1.keySet()) {
			dataset.addValue(hashmap2.get("" + j), series2, key);
			j++;
		}
		j = 0;
		for (String key : hashmap1.keySet()) {
			dataset.addValue(hashmap3.get("" + j), series3, key);
			j++;
		}

		return dataset;

	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return The dataset.
	 */
	private static CategoryDataset createDataset2() {

		// row keys...
		final String series1 = "Czwarty";

		// create the dataset...
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		int j = 0;
		for (String key : hashmap1.keySet()) {
			dataset.addValue(hashmap4.get("" + j), series1, key);
			j++;
		}

		return dataset;

	}

	@Override
	public void changeData() {
		this.setChart(createChart(getTitle()));
	}

	private static int random(int max) {
		return new Random().nextInt(max);
	}

	@SuppressWarnings("static-access")
	@Override
	public String getTitle() {
		return this.title;
	}

	@SuppressWarnings("static-access")
	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public HashMap<String, Integer> getHashMapDataSet() {
		return hashmap1;
	}

	@Override
	public void setHashMapDataSet(HashMap<String, Integer> hashMap) {
		if (hashMap != null) {
			hashmap1 = hashMap;
			changeData();
		}
	}

}
